package com.sansecy.customview.view.paystatus;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.sansecy.customview.DensityUtils;
import com.sansecy.customview.R;

import java.util.ArrayList;

public class PayStatusActivity extends AppCompatActivity {

    private PayStatusActivity mContext;
    private PayingView mPayingView;

    public static void start(Context context) {
        Intent starter = new Intent(context, PayStatusActivity.class);
//    starter.putExtra();
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_pay_status);
        final ViewGroup content = findViewById(R.id.contentPanel);
        View btnPayFailed = findViewById(R.id.btn_pay_failed);
        btnPayFailed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content.removeAllViews();
                PayFailedView failedView = new PayFailedView(mContext);
                failedView.setTag("PayFailedView");
                failedView.setColor(Color.parseColor("#FF5D48"));
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(200, 300);
                layoutParams.gravity = Gravity.CENTER;
                content.addView(failedView, layoutParams);
            }
        });
        View btnPaySuccess = findViewById(R.id.btn_pay_success);
        btnPaySuccess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content.removeAllViews();
                PaySuccessView successView = new PaySuccessView(mContext);
                successView.setColor(Color.parseColor("#476CC3"));
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(200, 300);
                layoutParams.gravity = Gravity.CENTER;
                content.addView(successView, layoutParams);
            }
        });
        View btnPaying = findViewById(R.id.btn_paying);
        btnPaying.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content.removeAllViews();
                mPayingView = new PayingView(mContext);
                ArrayList<String> colorList = new ArrayList<>();
                colorList.add("#547fdd");
                colorList.add("#e6547fdd");
                colorList.add("#cc547fdd");
                colorList.add("#b3547fdd");
                colorList.add("#99547fdd");
                colorList.add("#80547fdd");
                colorList.add("#66547fdd");
                colorList.add("#4d547fdd");
                colorList.add("#33547fdd");
                colorList.add("#1a547fdd");
                colorList.add("#12547fdd");
                colorList.add("#0d547fdd");

                mPayingView.setColorList(colorList);
                mPayingView.setItemRadius(DensityUtils.dp2px(mContext,15));
                mPayingView.setTag("PayingView");
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(185, 185);
                layoutParams.gravity = Gravity.CENTER;
                content.addView(mPayingView, layoutParams);
            }
        });
        View btnPayingToSuccess = findViewById(R.id.btn_paying_to_success);
        btnPayingToSuccess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PayFailedView payFailedView = content.findViewWithTag("PayFailedView");
                if (payFailedView != null) {
                    payFailedView.setVisibility(View.GONE);
                }
            }
        });
        View btnPayingToFailed = findViewById(R.id.btn_paying_to_failed);
        btnPayingToFailed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content.removeAllViews();
            }
        });
    }

    public void cancel(View view) {
        if (mPayingView != null) {
            mPayingView.stopAnimation();
        }
    }

    public void start(View view) {
        if (mPayingView != null) {
            mPayingView.startAnimation();
        }
    }

    public void show(View view) {
        Dialog dialog = new Dialog(mContext);
        mPayingView = new PayingView(mContext);
        ArrayList<String> colorList = new ArrayList<>();
        colorList.add("#547fdd");
        colorList.add("#e6547fdd");
        colorList.add("#cc547fdd");
        colorList.add("#b3547fdd");
        colorList.add("#99547fdd");
        colorList.add("#80547fdd");
        colorList.add("#66547fdd");
        colorList.add("#4d547fdd");
        colorList.add("#33547fdd");
        colorList.add("#1a547fdd");
        colorList.add("#12547fdd");
        colorList.add("#0d547fdd");

        mPayingView.setColorList(colorList);
        mPayingView.setItemRadius(DensityUtils.dp2px(mContext,15));
        mPayingView.setTag("PayingView");
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(185, 185);
        layoutParams.gravity = Gravity.CENTER;
        dialog.setContentView(mPayingView);
        dialog.show();
    }
}
